package com.tdt.alarm.model;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tdt.alarm.R;
import com.tdt.alarm.controller.fragment.Fragment2;

public class SongAdapter extends BaseAdapter {
	private ArrayList<Song> songs;
	private LayoutInflater songInf;

	public SongAdapter(Context c, ArrayList<Song> theSongs) {
		songs = theSongs;
		songInf = LayoutInflater.from(c);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return songs.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// map to song layout
		LinearLayout songLay = (LinearLayout) songInf.inflate(R.layout.song,
				parent, false);
		// get title and artist views
		TextView songView = (TextView) songLay.findViewById(R.id.song_title);
		TextView artistView = (TextView) songLay.findViewById(R.id.song_artist);
		final CheckBox isChoose = (CheckBox) songLay.findViewById(R.id.cbIsChoose);
		// get song using position
		final Song currSong = songs.get(position);
		// get title and artist strings
		songView.setText(currSong.getTitle());
		artistView.setText(currSong.getArtist());
		isChoose.setChecked(currSong.isChoose());
		// set position as tag
		songLay.setTag(position);
		
		isChoose.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Fragment2.setChoose(position, isChoose.isChecked());
			}
		});
		return songLay;
	}

}
